package com.dundun.SSO.controller.manage;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.dundun.SSO.core.base.BaseController;
import com.dundun.SSO.manage.ISsoGroupService;
import com.dundun.SSO.manage.pojo.SsoGroup;
import com.dundun.SSO.manage.query.SsoGroupQuery;
import com.dundun.SSO.pojo.Constant;
import com.dundun.SSO.pojo.ResponseBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/5/11 0011 17:50
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
@Controller
@RequestMapping("/sso/ssoGroup")
public class SsoGroupController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ISsoGroupService ssoGroupService;


    public String getPage(){
        return "ssoGroup";
    }

    @RequestMapping("/qryList.json")
    @ResponseBody
    public ResponseBean getSsoGroupList(@RequestBody SsoGroupQuery ssoGroupQuery){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<SsoGroup> ssoGroupList = ssoGroupService.selectByPage(ssoGroupQuery);
            int count = ssoGroupService.selectCountByPage(ssoGroupQuery);
            responseBean.setRows(ssoGroupList);
            responseBean.setTotal(count);
        }catch (Exception e){
            logger.error("getSsoGroupList",e);
        }finally {
            return responseBean;
        }
    }

    @RequestMapping("update.json")
    @ResponseBody
    public ResponseBean updateSsoGroup(@RequestBody SsoGroup ssoGroup){
        ResponseBean responseBean = new ResponseBean();
        try{
            if(ssoGroup.getId() == null){
                return ResponseBean.parameterError("id不能为空");
            }
            if(StringUtils.isBlank(ssoGroup.getName())){
                return ResponseBean.parameterError("名称不能为空");
            }
            if(ssoGroup.getParentId() != null){
                if(ssoGroup.getParentId() == ssoGroup.getId()){
                    return ResponseBean.parameterError("不能继承自身");
                }
                if(ssoGroupService.selectAllParentGroup(ssoGroup.getParentId()).contains(ssoGroup.getId())){
                    return ResponseBean.parameterError("不能继承下级组");
                }
            }
            if(ssoGroupService.updateByPrimaryKey(ssoGroup)>0){
                return ResponseBean.success(null,null);
            }else{
                return ResponseBean.unknowError(null,null);
            }
        }catch (Exception e){
            logger.error("updateSsoGroup",e);
        }
        return responseBean;
    }

    @RequestMapping("add.json")
    @ResponseBody
    public ResponseBean addSsoGroup(@RequestBody SsoGroup ssoGroup){
        ResponseBean responseBean = new ResponseBean();
        try{
            if(StringUtils.isBlank(ssoGroup.getName())){
                return ResponseBean.parameterError("名称不能为空");
            }
            ssoGroupService.saveNotNull(ssoGroup);
        }catch (Exception e){

        }
        return responseBean;
    }

    @RequestMapping("delete.json")
    @ResponseBody
    public ResponseBean deleteSsoGroup(@RequestBody Long[] ids,boolean deleteSubgroup){
        ResponseBean responseBean = new ResponseBean();
        if(deleteSubgroup){
            for(Long id:ids){
                ssoGroupService.delete(id);
            }
        }

        return responseBean;
    }
}
